//如何把前面例子中的“教授”对象也一并复制？
//让professor类实现cloneable接口，并在professor类中覆盖clone方法
package clone;

import org.apache.commons.lang.builder.ReflectionToStringBuilder;

class Student2 implements Cloneable//实现Cloneable接口,表明“能克隆”
{
	String name;
	int age;
	Professor2 p;
	
	Student2(String name,int age,Professor2 p)
	{
		this.name=name;
		this.age=age;
		this.p=p;
	}
	
	public Object clone()//覆盖基类的clone()方法，并声明为public（注意返回类型）。
	{
		//Object obj;
		Student2 o=null;
		try
		{
			o=(Student2)super.clone(); //调用super.clone()
		}catch(CloneNotSupportedException e)
		{
			e.printStackTrace();
		}
		o.p=(Professor2)p.clone();
		return o;
	}
	
	@Override
	public String toString() {
		return ReflectionToStringBuilder.toString(this);
	}
}

class Professor2 implements Cloneable
{
	String name;
	int age;
	Professor2(String name,int age)
	{
		this.age=age;
		this.name=name;
	}
	public Object clone()//覆盖基类的clone()方法，并声明为public（注意返回类型）。
	{
		Object obj=null;
		try
		{
			obj=super.clone(); //调用super.clone()
		}catch(CloneNotSupportedException e)
		{
			e.printStackTrace();
		}
		return obj;
	}
	
	@Override
	public String toString() {
		return ReflectionToStringBuilder.toString(this);
	}
}

class Test6
{
	public static void main(String[] args)
	{
		Professor2 p=new Professor2("wangwu",30);
		Student2 s1=new Student2("zhangsan",18,p);
		Student2 s2=(Student2)s1.clone();
		s2.name="lisi";
		s2.age=20;
		s2.p.age=50;
		s2.p.name="luliu";
		System.out.println("name="+s1.name+", age="+s1.age);
		System.out.println("name="+s2.name+", age="+s2.age);
		System.out.println("name="+s1.p.name+", age="+s1.p.age);
		System.out.println("name="+s2.p.name+", age="+s2.p.age);
		
	}
}